/**
 * 原料管理 MOCK 数据
 */
import mockjs from 'mockjs';

const startKey = "767654545434"

// 样本数据
const sampleItem = {
  key: startKey,
  mat_name: "咖啡粉",
  mat_brand: "星巴克",
  pack_spec: { type: "罐", value: 100, unit: "ML" }, // 包装规格
  pack_price: 128.98, // 整装的价格
  spec_price: 1.29, // 单位价格
  min_purchase: 10, // 最小起购量
  mat_norqgp: { value: 6, unit: "月" }, // 常规保质期
  mat_brkqgp: { value: 3, unit: "周" }, // 开封保质期
  mat_norstg: ["密封", "干燥", "避光"], // 常规存储方式
  mat_brkstg: ["密封", "干燥", "冷藏", "避光"], // 开封存储方式
  fact_name: "重庆鸿兴美克进出口商贸公司",
  fact_phone: "02386578976",
  mat_memo: "公司在成都有办事处",
  // 原料操作容器
  mat_cntrs: [{ name: "吧勺150ML", value: 15, unit: "g" }, { name: "吧勺250ML", value: 25, unit: "g" }],
  create_time: 1628072476,
  update_time: 1628072476,
  create_by: "小侠女"
}

// 构造50条测试数据
const mockDataSize = 20
const testData = []
let newItem
for (let i = 0; i < mockDataSize; i += 1) {
  newItem = JSON.parse(JSON.stringify(sampleItem))
  newItem.key = String(Number(newItem.key) + i)
  newItem.mat_name += i
  newItem.pack_spec.value += i
  newItem.pack_price += i
  newItem.min_purchase += i
  testData.push(newItem)
}

export default {
  'POST /api/material/list': (req, res) => {
    res.send(
      mockjs.mock(
        {
          ret: 0,
          data: {
            list: testData,
            total: testData.length,
            query: '',
          }
        }
      ),
    );
  },

  'POST /api/material/getData': (req, res) => {
    const { id } = req.body;
    let queryData = {}
    for(let i=0; i< testData.length; i += 1){
      if( testData[i].key == id )
        queryData = testData[i]
    }
    res.send(
      mockjs.mock(
        {
          ret: 0,
          data: {
            list: [queryData],
            total: 1,
            query: '',
          }
        }
      ),
    );
  },


  'POST /api/material/add': (req, res) => {
    const { data } = req.body;
    data.key = String(Number(startKey) + testData.length + 1)
    // 添加新数据
    testData.push(data)
    res.send(
      mockjs.mock(
        {
          ret: 0, // 通信成功
          data: {
            ret: 0, // 添加成功
          }
        }
      ),
    );
  },
};
